# -*- coding:utf-8 -*-
# @Author: shenyuyu
# @Time: 2023/6/29 15:16
# @File: qu_1_dataframe读写数据库.py
from pyspark.sql import SparkSession
from pyspark.sql.types import StructType, StringType, IntegerType, DoubleType

if __name__ == '__main__':
    spark = SparkSession.builder.appName("a").master("local[*]").getOrCreate()
    schema = StructType().\
        add("id", IntegerType(), True).\
        add("subject", StringType(), True).\
        add("score", DoubleType(), True)

    # df1 = spark.read.format("csv").\
    #     option("sep", ",").\
    #     option("header", False).\
    #     option("encoding", "utf-8").\
    #     schema(schema=schema).\
    #     load("file:///tmp/pycharm_project_161/data/sql/stu_score.txt")

    # df1.write.mode("overwrite").\
    #     format("jdbc").\
    #     option("url", "jdbc:mysql://hadoop1:3306/bigdata?useSSL=false&useUnicode=true&characterEncoding=UTF-8").\
    #     option("dbtable", "stu_score").\
    #     option("user", "root").\
    #     option("password", "123456").\
    #     save()

    df2 = spark.read.format("jdbc"). \
        option("url", "jdbc:mysql://hadoop1:3306/bigdata?useSSL=false&useUnicode=true&characterEncoding=UTF-8"). \
        option("dbtable", "stu_score"). \
        option("user", "root"). \
        option("password", "123456"). \
        load()

    df2.printSchema()
    df2.show()